<style lang="less">
	
</style>
<template>
    <div class="address" v-loading="loading">
		<group>
			<x-input title="姓　　名：" placeholder="请输入姓名" v-model="form.username"></x-input>
			<x-input title="电　　话：" placeholder="请输入电话" v-model="form.mobile"></x-input>
			<x-address label-width="84px" value-text-align="left" title="地　　址：" placeholder="请选择地址" :list="addressData" @on-shadow-change="addresschange" v-model="changeaddres"></x-address>
			<x-textarea title="详细地址：" placeholder="请输入详细地址" v-model="form.address"></x-textarea>
			<x-input title="邮政编码：" placeholder="请输入邮政编码" v-model="form.zipcode"></x-input>
			<x-switch title="是否默认：" :value-map="[0,1]" v-model="form.isdefault"></x-switch>
			<popup-radio label-width="84px" value-text-align="left" title="地址类型：" :options="addtype" v-model="form.type"></popup-radio>
		</group>
		<box gap="20px 10px">
			<x-button type="primary" :show-loading="submiloading" @click.native="submit">保存</x-button>
		</box>
	</div>
</template>

<script>
	import {ChinaAddressV4Data as addressData,PopupRadio} from 'vux'
    import { mapState,mapActions } from 'vuex';
    export default {
		name: "cart",
		components : {
			PopupRadio
		},
        data() {
            return {
				id : this.$route.params.id,
				loading : false,
				submiloading : false,
				changeaddres:[],
				addressData,
				addtype : [
					{
						key: 0,
						value: '家庭'
					},
					{
						key: 1,
						value: '公司'
					}
				],
                form : {
					username:'',
					mobile:'',
					zipcode:'',//邮编
					province_code:'',//省份代码
					city_code:'',//城市代码
					area_code:'',//区域代码
					street_code:0,//街道ID
					regions_name:'',//省/市/区文字
					address:'',//	详细地址
					isdefault:0,//是否默认
					type:0,//地址类型，0家庭，1公司
				}
            }
        },
        methods: {
			addresschange(ids, names){
				this.form.province_code = ids[0]
				this.form.city_code = ids[1]
				this.form.area_code = ids[2]
				this.form.regions_name = names.join('-')
			},
			submit(){
				if(this.id){
					this.$http.member.put('vipaddress/'+this.id,this.form).then(res => {
						if(!res.data.code){
							this.$message.success('修改成功')
							this.$store.dispatch('shop/initaddress',true);
							this.$router.push({name:'shop_address'})
						}else{
                            this.$message.error('修改失败')
						}
					})
				}else{
					this.$http.member.post('vipaddress',this.form).then(res => {
						if(!res.data.code){
							this.$message.success('添加成功')
							this.$store.dispatch('shop/initaddress',true);
							this.$router.push({name:'shop_address'})
						}else{
                            this.$message.error('添加失败')
						}
					})
				}
			}
        },
        computed: {
            data(){
                return this.$store.state.shop.address;
			}
        },
        mounted(){
            
        },
        created(){
            if(this.id){
				this.loading = true;
				this.$http.member.get('vipaddress/'+this.id).then(res => {
					this.loading = false;
					if(!res.data.code){
						this.form = res.data.data;
						this.changeaddres = [
							res.data.data.province_code+'',
							res.data.data.city_code+'',
							res.data.data.area_code+''
						]
					}
				})
			}
        }
    }
</script>

